<template>
  <div>
    <CategoryVue :scene="scene"></CategoryVue>

    <el-card style="margin-top: 10px">
      <div v-show="Mscene == 0">
        <el-button
          type="primary"
          size="default"
          icon="Plus"
          style="margin-bottom;: 10px"
          :disabled="CategoryStores.c3Id ? false : true"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <!-- 展示已有的spu数据 -->
        <el-table border :data="spuList" style="margin: 10px 0px">
          <el-table-column
            label="序号"
            width="100"
            fixed="left"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column
            label="SPU名称"
            align="center"
            prop="spuName"
          ></el-table-column>
          <el-table-column
            label="SPU描述"
            align="center"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="SPU操作"
            width="300"
            fixed="right"
            align="center"
          >
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="danger"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="success"
                size="small"
                icon="View"
                title="查看SKU列表"
                @click="findSku(row)"
              ></el-button>
              <el-popconfirm
                :title="`确定删除${row.spuName}吗？`"
                @confirm="deleteSpu(row)"
              >
                <template #reference>
                  <el-button
                    type="info"
                    size="small"
                    icon="Delete"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 10]"
          background
          layout=" prev, pager, next, jumper,->,total, sizes,"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="getSpu"
        />
      </div>
      <!--添加和修改spu的表单 -->
      <SpuFrom
        ref="spu"
        v-show="Mscene == 1"
        @changeScene="changeScene"
      ></SpuFrom>
      <!-- 添加sku的表单 -->
      <SkuFrom
        ref="sku"
        v-show="Mscene == 2"
        @changeScene="changeScene"
      ></SkuFrom>
      <el-dialog v-model="show" title="sku列表">
        <el-table border :data="skuArr">
          <el-table-column label="sku名字" prop="skuName"></el-table-column>
          <el-table-column label="sku价格" prop="price"></el-table-column>
          <el-table-column label="sku重量" prop="weight"></el-table-column>
          <el-table-column label="图片">
            <template #default="{ row }">
              <el-image
                style="width: 100px; height: 100px"
                :src="row.skuDefaultImg"
                fit="cover"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>
<script setup lang="ts">
import { onUnmounted, ref, watch } from 'vue'
//引入请求Spu接口
import { reqRemoveSpu, reqSkuInfo, reqhasSpu } from '@/api/product/spu/spu'
//引入分类的仓库
import Category from '@/stores/category'
import type {
  HasSpuResponseData,
  Records,
  SkuData,
  SpuData,
} from '@/api/product/spu/type'
import SpuFrom from './spuFrom.vue'
import SkuFrom from './skuFrom.vue'
import { ElMessage } from 'element-plus'
let CategoryStores = Category()
//当前页码
let pageNo = ref<number>(1)
//每页显示多少条
let pageSize = ref<number>(5)
let scene = ref<number>(1)
//总条数
let total = ref<number>(0)
let spuList = ref<Records>([])
//场景数据
let Mscene = ref<number>(0) //1 添加spu 2 添加sku 0显示spu列表
let spu = ref<any>()
let sku = ref<any>()
let skuArr = ref<SkuData[]>([])
let show = ref<boolean>(false)
//监听某个三级分类的改变
watch(
  () => CategoryStores.c3Id,
  () => {
    if (!CategoryStores.c3Id) return
    getSpu()
  },
)
//获取某个三级分类
const getSpu = async (pager = 1) => {
  pageNo.value = pager
  const res: HasSpuResponseData = await reqhasSpu(
    pageNo.value,
    pageSize.value,
    CategoryStores.c3Id,
  )
  if (res.code === 200) {
    spuList.value = res.data.records
    total.value = res.data.total
  }
}
//分页器下拉菜单改变
const handleSizeChange = () => {
  getSpu()
}
//添加spu
const addSpu = () => {
  spu.value.initAddSpu(CategoryStores.c3Id)
  Mscene.value = 1
}
//修改spu
const updateSpu = (row: SpuData) => {
  spu.value.initHasSpuData(row)

  Mscene.value = 1
}
//改变场景
const changeScene = (val: number) => {
  Mscene.value = val
}
//添加sku
const addSku = (row: SpuData) => {
  sku.value.initSkuData(CategoryStores.c1Id, CategoryStores.c2Id, row)
  Mscene.value = 2
}
onUnmounted(() => {
  CategoryStores.c1Id = ''
  CategoryStores.c2Id = ''
  CategoryStores.c3Id = ''
})
//显示sku
const findSku = async (row: SpuData) => {
  let req = await reqSkuInfo(row.id as number)

  if (req.code == 200) {
    skuArr.value = req.data
    show.value = true
  }
}
//删除spu
const deleteSpu = async (row: SpuData) => {
  const res = await reqRemoveSpu(row.id as number)
  if (res.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getSpu(pageNo.value)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>
<style scoped lang="scss"></style>
